﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.Common;
using ePlanet.ResourceRequest.DataAccess;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.Services;
using System.Data;

namespace ePlanet.ResourceRequest.Business
{
    public class BUSTempUser : BusinessBase, ITempUserService
    {
        private TempUserDAL m_TempUserDAL = null;

        public BUSTempUser()
        {
            if (this.m_TempUserDAL == null)
            {
                this.m_TempUserDAL = new TempUserDAL();
            }
        }

        public bool AddNewTempUser(TempUser user)
        {
            user.TEMPUSER[0].STATUSID = (int)TempUserStausEnum.Originale;
            return this.m_TempUserDAL.AddNewTempUser(user);
        }

        /// <summary>
        /// 检测账户是否存在
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public bool CheckUserLoginName(string loginName)
        {
            bool rtnBool = false;
            TempUser.TEMPUSERDataTable oTempUser = this.m_TempUserDAL.GetTempUserByLoginName(loginName);
            if (oTempUser != null && oTempUser.Rows.Count > 0)
            {
                rtnBool = true;
            }
            return rtnBool;
        }

        /// <summary>
        /// 验证临时用户登录
        /// </summary>
        /// <param name="strMsg"></param>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckLogin(out string strMsg, out string userID, string loginName, string password)
        {
            strMsg = string.Empty;
            userID = string.Empty;
            bool rtnBool = false;
            try
            {
                TempUser.TEMPUSERRow oRow = this.m_TempUserDAL.CheckLogin(loginName, password);
                if (oRow != null)
                {
                    if (oRow.EXPIRATIONTIME > DateTime.Now)
                    {
                        userID = oRow.TEMPUSERID;
                        rtnBool = true;
                    }
                    else
                    {
                        strMsg = "对不起，您的账户已经过期，请重新申请或者联系管理员。";
                    }

                    switch ((int)oRow.STATUSID)
                    {
                        case (int)TempUserStausEnum.CanLogin:
                            break;
                        case (int)TempUserStausEnum.CannotLogin:
                        case (int)TempUserStausEnum.Originale:
                            strMsg = "对不起，您的账户不允许登录，请联系管理员重新授权";
                            break;
                        default:
                            strMsg = "对不起，您的账户状态异常，请联系管理员重新核查。";
                            break;
                    }
                }
                else
                {
                    strMsg = "账号或密码错误";
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.Message;
            }
            return rtnBool;
        }

        /// <summary>
        /// 通过UserID查询临时用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public TempUser GetTempUserByUserID(string userID)
        {
            TempUser rtnUser = null;
            DataTable oDataTable = this.m_TempUserDAL.GetTempUserByUserID(userID);
            if (oDataTable != null && oDataTable.Rows.Count == 1)
            {
                rtnUser = new TempUser();
                rtnUser.Merge(oDataTable);
            }
            return rtnUser;
        }

        /// <summary>
        /// 设置临时用户可以登录
        /// </summary>
        /// <param name="user"></param>
        public void AgreeLogin(TempUser user)
        {
            this.m_TempUserDAL.AddNewTempUser(user);
            this.m_TempUserDAL.AgreeLogin_AddTempUserRelateInP_Auth_User(user);
        }

        public DataTable GetTempUserList(TempUserStausEnum statusEnum, string exameUserID)
        {
            return this.m_TempUserDAL.GetTempUserList(statusEnum, exameUserID);
        }


        public void DisAgreeLogin(TempUser user)
        {
            this.m_TempUserDAL.AddNewTempUser(user);
            this.m_TempUserDAL.DisAgreeLogin(user);
        }


        public TempUser.TEMPUSERRow GetUser(string loginName, string password)
        {
            return this.m_TempUserDAL.CheckLogin(loginName, password);
        }


        public bool UpdateUser(TempUser user)
        {
            return this.m_TempUserDAL.UpdateUser(user);
        }


        public bool ResetPassword(TempUser.TEMPUSERRow user)
        {
            return this.m_TempUserDAL.ResetPassword(user);
        }


        public bool DelayUser(TempUser.TEMPUSERRow user)
        {
            return this.m_TempUserDAL.DelayUser(user);
        }
    }
}
